﻿using Grasshopper.Kernel;
using System;

namespace D004_Component
{
    public class Component_CreateHuman : GH_Component, IGH_InitCodeAware
    {
        protected override System.Drawing.Bitmap Icon => Properties.Resources.human;
        public override Guid ComponentGuid => new Guid("26DC253A-74B4-43C1-9E48-7609DA88586D");

        public Component_CreateHuman()
          : base("CreateHuman", "ch",
              "创建一个人",
              "PluginTest", "Test")
        {
        }

        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("姓名", "n", "人的名字", GH_ParamAccess.item);
            pManager.AddIntegerParameter("年龄", "a", "人的年龄", GH_ParamAccess.item);
            pManager.AddIntegerParameter("ID", "i", "编号", GH_ParamAccess.item);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddParameter(new Param_Human(), "人", "r", "一个人", GH_ParamAccess.item);
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // 获取输入
            string name = string.Empty;
            int age = 0;
            int id = 0;

            DA.GetData(0, ref name);
            DA.GetData(1, ref age);
            DA.GetData(2, ref id);

            // 逻辑代码
            var h = new GH_Human();
            h.Value.Name = name;
            h.Value.Age = age;
            h.Value.Id = id;

            // 设置输出
            DA.SetData(0, h);
        }

        public void SetInitCode(string code)
        {
            var cs = code.Split(' ');
            switch (cs.Length)
            {
                case 1:
                    AssignInitCodeToInputParameter(0, cs[0]);
                    break;
                case 2:
                    AssignInitCodeToInputParameter(0, cs[0]);
                    AssignInitCodeToInputParameter(1, cs[1]);
                    break;
                case 3:
                    AssignInitCodeToInputParameter(0, cs[0]);
                    AssignInitCodeToInputParameter(1, cs[1]);
                    AssignInitCodeToInputParameter(2, cs[2]);
                    break;
            }
        }
    }
}